package com.net.xpay.common.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.enums.partner.PartnerUserFilingsAuditStatus;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.model.PartnerUserFilingsSubmitModel;
import com.net.xpay.common.model.RegisterModel;
import com.net.xpay.common.model.search.PartnerUserFilingsSearchModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.PhoneUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.manager.partner.PartnerUserFilingsManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
@Component
public class PartnerUserFilingsValidator {
    @Autowired
    private UserManager userManager;

    @Autowired
    private PartnerUserFilingsManager partnerUserFilingsManager;


    public void validatePartnerUserFilings(Partner partner, PartnerUserFilingsSubmitModel model) {
        log.info("validatePartnerUserFilings partnerId={}|params={}|", partner.getId(), SerializationUtil.obj2String(model));

        if (Strings.isNullOrEmpty(model.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"请输入注册手机号");
        }
        if (!PhoneUtil.isEffectiveMobile(model.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"注册手机号格式不正确");
        }
        if (Strings.isNullOrEmpty(model.getBusinessName())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"请输入企业名称");
        }
//        if (Strings.isNullOrEmpty(model.getBusinessLicense())){
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"法人营业执照不可为空");
//        }
//        if (Strings.isNullOrEmpty(model.getBizSceneDesc())){
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"业务场景说明书加公章不可为空");
//        }
//        if (model.getServiceRatio() == null){
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"请输入到户费率");
//        }
//        if (model.getServiceRatio().compareTo(new BigDecimal("0")) < 0) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"到户费率不能小于0");
//        }
//        model.setServiceRatio(model.getServiceRatio().setScale(2, RoundingMode.HALF_UP));

        //去除空格
        model.setMobile(model.getMobile().trim());
        model.setBusinessName(model.getBusinessName().trim());

        User user = userManager.findByMobile(model.getMobile());
        if (user != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "注册手机号已被使用");
        }
        RegisterModel registerModel = new RegisterModel();
        registerModel.setName(model.getBusinessName());
        User selected = userManager.findByName(registerModel.getName());
        if (selected != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "同名商户已被注册");
        }

        PartnerUserFilingsSearchModel searchModel = new PartnerUserFilingsSearchModel();
        searchModel.setPartnerId(partner.getId());
        searchModel.setAuditStatus(PartnerUserFilingsAuditStatus.AUDITING);
        searchModel.setMobile(model.getMobile());
        long count = partnerUserFilingsManager.searchCount(searchModel);
        if (count > 0L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该商户已提交待审核，请勿重复提交");
        }
    }
}
